package com.yuanli.cosmeticmirror.model.impl;

import android.text.TextUtils;

import androidx.lifecycle.LifecycleOwner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.comenjoysoft.baselibrary.listener.CallbackListener;
import com.comenjoysoft.baselibrary.util.SPUtils;
import com.orhanobut.logger.Logger;
import com.rxjava.rxlife.BaseScope;
import com.rxjava.rxlife.RxLife;
import com.yuanli.cosmeticmirror.base.Constant;
import com.yuanli.cosmeticmirror.base.HttpApi;
import com.yuanli.cosmeticmirror.bean.BaseOneBean;
import com.yuanli.cosmeticmirror.bean.FeedbackBean;
import com.yuanli.cosmeticmirror.bean.UnifyBean;
import com.yuanli.cosmeticmirror.model.biz.IFeedBackBiz;

import rxhttp.wrapper.param.RxHttp;

public class FeedbackImpl extends BaseScope implements IFeedBackBiz {
    public FeedbackImpl(LifecycleOwner owner) {
        super(owner);
    }


    @Override
    public void getToken(CallbackListener<String> listener) {
        RxHttp.get(HttpApi.GET_IMG_TOKEN)
                .asString()
                .as(RxLife.asOnMain(this))
                .subscribe(s -> {
                            Logger.d("getToken onSucess :" + s);
                    FeedbackBean s1 = JSON.parseObject(s, FeedbackBean.class);
                            if (!TextUtils.isEmpty(s1.getToken())) {
                                listener.success(s1.getToken());
                            } else {
                                listener.failed(null);
                            }
                        }, throwable -> {
                            Logger.d("getToken onFailed :" + throwable.getMessage());
                            listener.failed(null);
                        }
                );
    }

    @Override
    public void uploadImg(String describe, String imgUrl, CallbackListener<UnifyBean> listener) {
        RxHttp.postJson(HttpApi.POST_FEEDBACK)
                .add("userid", SPUtils.getInstance().getString(Constant.SP_USER_ACCOUNT))
                .add("describ", describe)
                .add("imgUrl", imgUrl)
                .asString()
                .as(RxLife.asOnMain(this))
                .subscribe(s -> {
                    Logger.d("uploadImg onSucess :" + s);
                    if (!TextUtils.isEmpty(s)) {
                        UnifyBean s1 = JSON.parseObject(s, UnifyBean.class);
                        if (s1.getCode() == 1) {
                            listener.success(s1);
                        } else {
                            listener.failed(null);
                        }
                    } else {
                        listener.failed(null);
                    }
                }, throwable -> {
                    Logger.d("uploadImg onFailed :" + throwable.getMessage());
                    listener.failed(null);
                });
    }


}
